package day14;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Scanner;

/**
 * Created by kunsb on 2016/10/17.
 */
public class Explorer {

    public static void main(String[] args) {
        File file =new File("C:\\Users\\kunsb\\Desktop");

        Explorer fileTest = new Explorer();
        Scanner scanner = new Scanner(System.in);
        while (true){
            System.out.println("路径是：" + file.getPath());
            File[] listFiles = file.listFiles();
            fileTest.getMenu(listFiles);
            int read =scanner.nextInt();
            if (read<0|| read > listFiles.length+3){
                System.out.println("wrong");
                continue;
            }else if (read==0){
                break;
            }else if (read==1){
                String top=file.getPath().substring(0,3);
                file = new File(top);
                continue;
            }else if (read==2){
                if (file.getParentFile()!=null) {
                    file =  file.getParentFile();
                }
                continue;
            }
            File select = listFiles[read - 3];
            if (select.isDirectory()){
                file=select;
            }else {
                System.out.println("请输入操作：1，复制  2，移动  3，删除文件 4 ，查看文件");
                int read1 =scanner.nextInt();

                if (read1==1){
                    System.out.println("请输入复制到的文件路径：");
                    String road = scanner.nextLine();
                    File path =new File(road);
                    if (!path.exists()){
                        path.mkdirs();//创建该路径中所有不存在的文件夹
                    }
                    System.out.println("请输入文件名：");
                    String newName = scanner.nextLine();
                    if (newName == null || "".equals(newName)) {
                        newName = select.getName();
                    }
                    File target = new File(road + "\\" + newName);
                    int i = 1;
                    while (target.exists()){
                        String[] split = newName.split("\\.");
                        target = new File(road + "\\" + split[0] + i + "." + split[1]);
                        i++;
                    }
                    FileInputStream bili =null;
                    FileOutputStream out =null;
                    try {
                        out = new FileOutputStream(target);
                        bili = new FileInputStream(select);
                        int bi;
                        byte[] bis = new byte[1024*10];
                        while ((bi = bili.read(bis)) != -1)
                            out.write(bis);
                    } catch (Exception e) {
                        e.printStackTrace();
                    } finally {
                        try {
                            if (bili != null)
                                bili.close();
                            if (out != null)
                                out.close();
                        } catch (IOException e) {
                        }
                    }
                }else if (read1==2){
                    System.out.println("请输入要移动到的文件夹：");
                    String road = scanner.nextLine();
                }else if (read1==3){
                    select.delete();
                }else if (read1==4){
                    System.out.println("名字："+ select.getName());
                    System.out.println("长度："+ select.length() + "字节+");
                }
                /*
                菜单：1.复制
                    2. 移动
                    3. 删除
                    0. 我就看看 （打印文件信息）
                 */
//                System.out.println("名字："+ select.getName());
//                System.out.println("长度："+ select.length() + "字节+");
            }

        }

    }
    public void getMenu(File[] file){
        System.out.println("0: 退出");
        System.out.println("1: 返回顶部");
        System.out.println("2: 返回上一层");
        for (int i = 0; i <file.length ; i++) {
            System.out.println(i+3 + ": " + file[i].getName());
        }
    }
}
